package druid;

import com.alibaba.druid.pool.DruidDataSourceFactory;
import entity.User;

import javax.sql.DataSource;
import java.io.*;
import java.sql.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * 将.txt文件中的数据和数据库中匹配并打印到控制台
 */
public class Test {
    public static void main(String[] args) {

        Connection conn = null;
        try {
            conn = getConnection();
            String sql = "select * from test1";
            Map<Integer, User> map = getMap();
            result(conn,sql,map);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 返回结果
     * @param conn
     * @param sql
     * @param map
     */
    public static void result (Connection conn,String sql,Map<Integer,User> map){

        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            while(rs.next()){
                if (map.containsKey(rs.getInt("id"))){
                    Integer id = rs.getInt("id");
                    User user = new User();
                    user.setId(id);
                    user.setName(rs.getString("name"));
                    user.setAge(rs.getInt("age"));
                    user.setSex(rs.getString("sex"));
                    user.setPhone(map.get(id).getPhone());
                    user.setCity(map.get(id).getCity());
                    System.out.println(user);
                }

            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            close(conn,rs,ps);
        }

    }

    /**
     * 返回在文件中读取出来的数据
     * @return
     * @throws IOException
     */
    public static Map<Integer , User> getMap() throws IOException {
        Map<Integer , User> map = new HashMap<>();
        File file = new File("test1.txt");
        FileReader fr = new FileReader(file);
        BufferedReader br = new BufferedReader(fr);
        String line = null;
        br.readLine();
        while( (line = br.readLine()) != null){
            User user = new User();
            String[] split = line.split("\\,");
            user.setCity(split[2]);
            user.setPhone(split[1]);
            map.put(Integer.parseInt(split[0]),user);
        }
        br.close();
        fr.close();
        return map;
    }
    /**
     * 获取连接
     * @return
     * @throws Exception
     */
    public static Connection getConnection() throws Exception {
            Properties pro = new Properties();
            pro.load(Test.class.getClassLoader().getResourceAsStream("druid.properties"));
            DataSource ds = DruidDataSourceFactory.createDataSource(pro);
            return ds.getConnection();
    }

    /**
     * 关闭资源
     * @param connection
     * @param resultSet
     * @param statement
     */
    public static void close(Connection connection, ResultSet resultSet, Statement statement){
        if (resultSet != null){
            try {
                resultSet.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (statement != null){
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (connection != null){
            try {
                connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
}
